package com.moshang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.core.constant.Operation;
import com.moshang.blog.entity.SysDict;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.mapper.SysDictMapper;
import com.moshang.blog.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: blog
 * @description: ${description}
 * @author: xieweiwei
 * @create: 2018-11-15 15:03
 **/
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    @Resource
    private SysDictMapper sysDictMapper;
    @Override
    public List<String> findDistinctType() {
        return sysDictMapper.findDistinctType();
    }

    @Override
    public void saveOrUpdateDict(SysDict sysDict,String type) {
        if (Operation.INSERT.equals(type)) {
            sysDict.setCreateBy(MySysUser.currentUser().getId());
            sysDict.setUpdateBy(MySysUser.currentUser().getId());
            sysDict.setCreateTime(new Date());
            sysDict.setUpdateTime(new Date());
            sysDict.setCreateName(MySysUser.userName());
            sysDict.setUpdateName(MySysUser.userName());
            sysDict.setIsActivited(0);
        }else {
            sysDict.setUpdateBy(MySysUser.currentUser().getId());
            sysDict.setUpdateName(MySysUser.userName());
        }
        saveOrUpdate(sysDict);
    }

    @Override
    public List<SysDict> findByCondition(String type) {
        QueryWrapper<SysDict> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("type",type);
        entityWrapper.eq("is_activited",0);
        List<SysDict> list=baseMapper.selectList(entityWrapper);
        return list;
    }

    @Override
    public SysDict selectByCondition(String type, String value) {
        QueryWrapper<SysDict> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("type",type);
        entityWrapper.eq("value",value);
        entityWrapper.eq("is_activited",0);
        List<SysDict> list=baseMapper.selectList(entityWrapper);
        if (list.size()>0) {
            return list.get(0);
        }
        return null;
    }
}
